package cn.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.bean.Film_info;
import cn.bean.Hall;
import cn.bean.Play;
import cn.dao.HallDao;
import cn.dao.PlayDao;
import cn.dbc.BaseDao;

public class PlayDaoImpl implements PlayDao
{
	BaseDao bs = new BaseDao();
	Film_infoDaoImpl fi = new Film_infoDaoImpl();
	HallDaoImpl hd = new HallDaoImpl();
	 
	

	@Override
	public boolean delPlay(int id)
	{
		String sql = "delete from play where id = "+id;
		List<Object> ls = new ArrayList<Object>();
		boolean isFlag = bs.update(sql, ls);
		return isFlag;
	}

	@Override
	public List<Play> findPlayByInfo_id(int id)
	{
		List<Play> list = new ArrayList<Play>();
		Connection conn = bs.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		if(conn!=null){
			String sql = "select * from play where fk_film_info_id="+id;
			try {
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				
				while(rs.next()){
					Play p = new Play();
					p.setId(rs.getInt(1));
					p.setTime(rs.getString(2));
					p.setFilm_info(fi.findFilmById(rs.getInt(3)));
					p.setPrice(rs.getDouble(4));
					p.setEdition(rs.getString(5));
					p.setHall(hd.findHallById(rs.getInt(6)));
					list.add(p);
					
				}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			finally{
				bs.closeConnection(conn, pstmt, rs);
			}
		}
		
		
		return list;
	}

	@Override
	public boolean doPlay(String time, Film_info f, double price, String edition,Hall hall) {
		String sql = "insert into play(time,fk_film_info_id,price,edition,fk_hall_id) values(?,?,?,?,?)";
		List<Object> ls = new ArrayList<Object>();
		ls.add(time);
		ls.add(f.getId());
		ls.add(price);
		ls.add(edition);
		ls.add(hall.getId());
		boolean isFlag = bs.update(sql, ls);
		return isFlag;
	
	}

	@Override
	public Play findPlayById(int id)
	{
		Play p = new Play();
		Connection conn = bs.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		if(conn!=null){
			String sql = "select * from play where id="+id;
			try {
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
					if(rs.next()){
					p.setId(rs.getInt(1));
					p.setTime(rs.getString(2));
					p.setFilm_info(fi.findFilmById(rs.getInt(3)));
					p.setPrice(rs.getDouble(4));
					p.setEdition(rs.getString(5));
					p.setHall(hd.findHallById(rs.getInt(6)));
				}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			finally{
				bs.closeConnection(conn, pstmt, rs);
			}
		}
		
		
		return p;
	}

	@Override
	public List<Play> findAllPlay() {
		List<Play> list = new ArrayList<Play>();
		Connection conn = bs.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		if(conn!=null){
			String sql = "select * from play ";
			try {
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				
				while(rs.next()){
					Play p = new Play();
					p.setId(rs.getInt(1));
					p.setTime(rs.getString(2));
					p.setFilm_info(fi.findFilmById(rs.getInt(3)));
					p.setPrice(rs.getDouble(4));
					p.setEdition(rs.getString(5));
					p.setHall(hd.findHallById(rs.getInt(6)));
					list.add(p);
					
				}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			finally{
				bs.closeConnection(conn, pstmt, rs);
			}
		}
		
		
		return list;
	}

	@Override
	public List<Play> findAllPlayById(int id) {
		List<Play> list = new ArrayList<Play>();
		Connection conn = bs.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		if(conn!=null){
			String sql = "select * from play where fk_hall_id="+id;
			try {
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				
				while(rs.next()){
					Play p = new Play();
					p.setId(rs.getInt(1));
					p.setTime(rs.getString(2));
					p.setFilm_info(fi.findFilmById(rs.getInt(3)));
					p.setPrice(rs.getDouble(4));
					p.setEdition(rs.getString(5));
					p.setHall(hd.findHallById(rs.getInt(6)));
					list.add(p);
					
				}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			finally{
				bs.closeConnection(conn, pstmt, rs);
			}
		}
		
		
		return list;

	}

	@Override
	public boolean updatePlay(int id, String time, Film_info film_info,
			double price, String edition, Hall hall) {
		boolean isFlag=false;
		Connection conn = bs.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		if(conn!=null)
		{
			String sql="update play set time=?,fk_film_info_id=?,price=?,edition=?,fk_hall_id=? where id=?";
			try{
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, time);
			pstmt.setInt(2, film_info.getId());
			pstmt.setDouble(3, price);
			pstmt.setString(4, edition);
			pstmt.setInt(5,hall.getId());
			pstmt.setInt(6, id);
			int num=pstmt.executeUpdate();
			if(num>0)
			{
				isFlag=true;
			}
			}
			catch(SQLException e)
			{
				e.printStackTrace();
			}
		}
		return isFlag;
	}

}
